﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace Possan.Localization.Scanner
{
	class Program
	{
		static void banner()
		{
			Console.WriteLine("Possan.Localization.Scanner (C# version)");
		}

		static void syntax()
		{
			Console.WriteLine("usage: possan.localization.scanner.exe [library-file.xml] [defaultlocale or '-'] [filemask] {filemask} {filemask} ...");
		}

		static void Main(string[] args)
		{
			banner();

			if (args.Length < 3)
			{
				syntax();
			}
			else
			{

				string libxmlfile = args[0];

				XmlLibraryProvider lib = new XmlLibraryProvider();

				if (File.Exists(libxmlfile))
				{
					Console.WriteLine("Importing library: " + libxmlfile);

					lib.ImportXML(libxmlfile);
				}
				else
				{
					Console.WriteLine("Library doesn't exist, creating a new one.");
				}

				string defaultlocale = args[1];
				if (defaultlocale != "-")
				{
					lib.DefaultLocale = CultureInfo.GetCultureInfo(defaultlocale);
					Console.WriteLine("Setting default locale: " + lib.DefaultLocale.Name + " (" + lib.DefaultLocale.NativeName + ")");
				}
				else
				{
					Console.WriteLine("Using default locale in file: " + lib.DefaultLocale.Name + " (" + lib.DefaultLocale.NativeName + ")");
				}

				CodeScanner cs = new CodeScanner(lib);

				FileWalker fw = new FileWalker(cs);

				for (int k = 2; k < args.Length; k++)
					fw.Walk(args[k]);

				Console.WriteLine("Writing library: " + libxmlfile);
				lib.ExportXML(libxmlfile);
			}
		}
	}
}
